package com.cat.slidingWindow;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-the-longest-semi-repetitive-substring/
 * @create 2025/9/17 10:01
 * @since JDK17
 */

public class Solution34 {
    public int longestSemiRepetitiveSubstring(String s) {
        char[] arr = s.toCharArray();
        int n = arr.length, ans = 0, cnt = 0;
        for (int l = 0, r = 1; r < n; ) {
            while (r < n && cnt < 2) {
                cnt += arr[r] == arr[r - 1] ? 1 : 0;
                r++;
            }
            ans = Math.max(ans, r - l - 1);
            cnt -= arr[l] == arr[l + 1] ? 1 : 0;
            l++;
        }

        return ans;
    }
}
